package inst

import "errors"

type RoundRobinBanlance struct {
	remotes []string
	current int
}

func NewRoundRobinBanlance() *RoundRobinBanlance {
	return &RoundRobinBanlance{}
}
func (rb *RoundRobinBanlance) Add(remote string) error {
	if remote == "" {
		return errors.New("remote is invalid")
	}
	rb.remotes = append(rb.remotes, remote)
	return nil
}
func (rb *RoundRobinBanlance) Next() string {
	if len(rb.remotes) == 0 {
		return ""
	}
	res := rb.remotes[rb.current]
	rb.current = (rb.current + 1) % len(rb.remotes)
	return res
}

func (rb *RoundRobinBanlance) Get() (string, error) {
	return rb.Next(), nil
}
